//
//  WLJSON.m
//  cakeShop
//
//  Created by scjh on 15/2/13.
//  Copyright (c) 2015年 John. All rights reserved.
//

#import "WLJSON.h"

@implementation WLJSON

+ (NSString *)arrayToJson:(NSArray *)actorhalls {
    
    NSMutableString *jsonString = [[NSMutableString alloc] initWithString:@"["];
    
    for(NSDictionary *hall in actorhalls){
        
        //2. 遍历数组，取出键值对并按json格式存放
        NSMutableString *string = [NSMutableString string];
        
        [string appendString:@"{"];
        for (int i = 0; i < hall.allKeys.count; i ++) {
            
            [string appendString:[NSString stringWithFormat:@"\"%@\":\"%@\"",hall.allKeys[i],hall[hall.allKeys[i]]]];
            if (i < hall.allKeys.count - 1) {
                
                [string appendString:@","];
            }
        }
        [string appendString:@"},"];
        [jsonString appendString:string];
        
    }
    // 3. 获取末尾逗号所在位置
    NSUInteger location = [jsonString length]-1;
    
    NSRange range       = NSMakeRange(location, 1);
    
    // 4. 将末尾逗号换成结束的]}
    [jsonString replaceCharactersInRange:range withString:@"]"];
    
//    NSLog(@"jsonString = %@",jsonString);
    return jsonString;
}

//字典转Json
+ (NSString*)dictionaryToJson:(NSDictionary *)dic
{
    NSError *parseError = nil;
    NSData  *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

+ (NSString *)arrayOneToJson:(NSArray *)oneArr {
    
    NSMutableString *jsonString = [[NSMutableString alloc] initWithString:@"["];
    for (int i = 0; i < oneArr.count; i ++) {
        if (i != 0) {
            
            [jsonString appendString:@","];
        }
        [jsonString appendString:oneArr[i]];
    }
    [jsonString appendString:@"]"];
    return jsonString;
}

@end
